package com.gs.synedocket.util;

import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.callback.IOnSyncListener;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.HandleCode;
import com.gs.synedocket.constant.SyncStatus;
import com.gs.synedocket.db.dao.LogUtil;
import com.gs.synedocket.db.dto.MenuDto;
import com.gs.synedocket.db.entity.ParamType;
import com.gs.synedocket.db.entity.Truck;
import com.gs.synedocket.db.entity.UserTruck;
import com.gs.synedocket.db.sqlite.DBApi;
import com.gs.synedocket.presenter.SyncTablePresenter;
import com.gs.synedocket.view.ISyncTable;

import java.util.List;

public class SyncTableUtil implements ISyncTable {
    private static volatile SyncTableUtil instance;
    private final String[] syncTable = new String[]{"Trucks", "userTrucks", "parmsTypes", "menus"};
    private final Class[] syncClasses = new Class[]{Truck.class, UserTruck.class, ParamType.class, MenuDto.class};
    private int syncPosition;

    private final SyncTablePresenter mPresenter;

    public static SyncTableUtil getInstance() {
        if (instance == null) {
            synchronized (SyncTableUtil.class) {
                if (instance == null) {
                    instance = new SyncTableUtil();
                }
            }
        }
        return instance;
    }

    public SyncTableUtil() {
        mPresenter = new SyncTablePresenter();
        mPresenter.attachView(this);
    }

    private String getSyncTable() {
        return syncTable[syncPosition];
    }

    private Class getSyncClass() {
        return syncClasses[syncPosition];
    }

    public void download() {
        syncPosition = 0;
        ThreadManager.getInstance().execute(() -> {
            // 先清空旧数据
            DBApi.getInstance().deleteAll();
            DBApi.getInstance().clearQueryCacheMap();
            mPresenter.syncModel(getSyncTable(), getSyncClass());
        });
    }

    @Override
    public void onSyncTableSuccess(ApiResultData<List<Object>> apiResultData) {
        if (apiResultData.getStatus() != 2000) {
            downloadError(apiResultData.getMessage());
            return;
        }
        if (apiResultData.getData() != null) {
            syncInsertTable(apiResultData.getData());
        }
        syncPosition += 1;
        if (syncPosition < syncTable.length) {
            mPresenter.syncModel(getSyncTable(), getSyncClass());
            return;
        }
        downloadSuccess();
    }

    @Override
    public void onSyncTableFails(Exception e) {
        downloadFail();
    }

    private void syncInsertTable(Object data) {
        switch (syncPosition) {
            case 0:
                DBApi.getInstance().insertTruck((List<Truck>) data);
                break;
            case 1:
                DBApi.getInstance().insertUserTruck((List<UserTruck>) data);
                break;
            case 2:
                DBApi.getInstance().insertParam((List<ParamType>) data);
                break;
            case 3:
                DBApi.getInstance().insertMenu((List<MenuDto>) data);
                break;
        }
    }

    private void downloadFail() {
        SyncStatus.setSync(false);
        onSyncListener.onSyncListener(HandleCode.MSG_DOWNLOAD_TABLE_FAIL, null);
    }

    private void downloadError(String msg) {
        SyncStatus.setSync(false);
        LogUtil.getInstance().insertOrReplaceData("下載數據失敗：" + msg);
        onSyncListener.onSyncListener(HandleCode.MSG_DOWNLOAD_TABLE_FAIL, msg);
    }

    private void downloadSuccess() {
        SyncStatus.setSync(false);
        onSyncListener.onSyncListener(HandleCode.MSG_DOWNLOAD_TABLE_SUCCESS, null);
    }

    private IOnSyncListener onSyncListener;

    public void setOnSyncListener(IOnSyncListener onSyncListener) {
        this.onSyncListener = onSyncListener;
    }
}
